/**
* Copyright 2011 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.bricket.plugin.multibox.web;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import org.apache.wicket.ResourceReference;
import org.apache.wicket.markup.html.CSSPackageResource;
import org.apache.wicket.markup.html.JavascriptPackageResource;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.model.IModel;
import org.bricket.service.UncheckedRuntimeException;
import org.bricket.web.BricketApplication;
import org.bricket.web.BricketConfigAwarePanel;
import org.bricket.web.BricketDomReadyJavascriptBehaviour;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import brix.jcr.api.JcrNodeIterator;
import brix.jcr.wrapper.BrixNode;
import brix.web.reference.Reference;
import brix.web.reference.Reference.Type;
/**
* @author Ingo Renner
* @author Henning Teek
*/
public class MultiboxPanel extends BricketConfigAwarePanel<MultiboxConfig> {
private static final class ImageObjectOrderComparator implements Comparator<ImageObject>, Serializable {
@Override
public int compare(ImageObject o1, ImageObject o2) {
if (o1.getOrder() < o2.getOrder()) {
return -1;
} else if (o1.getOrder() > o2.getOrder()) {
return 1;
}
return 0;
}
}
private final Logger log = LoggerFactory.getLogger(MultiboxPanel.class);
private int counter;
private static final String FLV_PLAYER_SWF = "flvplayer.swf";
private static final ResourceReference FLV_PLAYER = new ResourceReference(MultiboxPanel.class,
"resources/multibox/files/" + FLV_PLAYER_SWF);
public MultiboxPanel(final String id, IModel<BrixNode> tileNode) {
super(id, tileNode, new MultiboxConfig());
}
@Override
protected void initGui() {
if (!getConfig().getReference().getType().equals(Type.NODE)) {
throw new UncheckedRuntimeException("Config must be of type node.");
}
List<ImageObject> imgObjects = createImageList(getConfig());
this.counter = 0;
add(CSSPackageResource.getHeaderContribution(MultiboxPanel.class, "resources/multibox/multibox.css"));
add(CSSPackageResource.getHeaderContribution(MultiboxPanel.class, "MultiboxPanel.css"));
add(JavascriptPackageResource.getHeaderContribution(MultiboxPanel.class, "resources/_common/js/mootools.js"));
add(JavascriptPackageResource.getHeaderContribution(MultiboxPanel.class, "resources/multibox/overlay.js"));
add(JavascriptPackageResource.getHeaderContribution(MultiboxPanel.class, "resources/multibox/multibox.js"));
final String mbId = "mb-" + getId();
final Map<String, Object> vars = new HashMap<String, Object>();
vars.put("mbId", mbId);
vars.put("useOverlay", getConfig().getUseOverlay());
vars.put("container", "document.body");
vars.put("contentColor", getConfig().getContentColor());
vars.put("showNumbers", getConfig().getShowNumbers());
vars.put("openFromLink", getConfig().getOpenFromLink());
vars.put("descClassName", "multiBoxDesc");
String pathToFlashplayer = urlFor(FLV_PLAYER).toString();
vars.put("path", pathToFlashplayer.substring(0, pathToFlashplayer.lastIndexOf(FLV_PLAYER_SWF)));
add(new BricketDomReadyJavascriptBehaviour(MultiboxPanel.class, "MultiboxPanel.js", vars));
add(new ListView<ImageObject>("listview", imgObjects) {
@Override
protected void populateItem(ListItem<ImageObject> item) {
item.add(new MultiboxItemPanel("imageObject", getCounter(), item.getModel(), mbId)
.setRenderBodyOnly(true));
}
});
}
private List<ImageObject> createImageList(MultiboxConfig conf) {
Reference ref = conf.getReference();
BrixNode dataNode = ref.getNodeModel().getObject();
// Erwartet wird ein Folder Namens thumb
// DataNode enthält die Layoutbilder, thumbs die gleichnamigen
// Thumbnails
// Für *.flv Dateien befindet sich in thumbs jeweils eine *.png Datei
List<String> slides = new ArrayList<String>();
List<String> thumbs = new ArrayList<String>();
Properties mbProperties = new Properties();
for (JcrNodeIterator iterator = dataNode.getNodes(); iterator.hasNext();) {
BrixNode node = new BrixNode((Node) iterator.next(), dataNode.getSession());
if (node.getPath().endsWith("/mb.properties")) {
try {
mbProperties.load(node.getNode(Property.JCR_CONTENT).getProperty("jcr:data").getBinary()
.getStream());
} catch (RepositoryException e) {
log.error("error retrieving properties from jcr.", e);
} catch (IOException e) {
log.error("error creating properties.", e);
}
} else if (node.getPath().endsWith("/thumb")) {
for (JcrNodeIterator subit = node.getNodes(); subit.hasNext();) {
BrixNode thumb = new BrixNode((Node) subit.next(), dataNode.getSession());
final String url = ((BricketApplication) getApplication()).getBrixLinkUrl(thumb, getRequestCycle());
thumbs.add(url);
}
} else {
final String url = ((BricketApplication) getApplication()).getBrixLinkUrl(node, getRequestCycle());
slides.add(url);
}
}
List<ImageObject> res = new ArrayList<ImageObject>();
for (String slide : slides) {
if (slide != null) {
String title = slide.substring(slide.lastIndexOf('/') + 1);
int order = Integer.MAX_VALUE;
try {
order = Integer.valueOf(mbProperties.getProperty(title + ".order"));
} catch (NumberFormatException nfe) {
log.error("invalid order value for: " + slide);
}
res.add(new ImageObject(slide, findThumb(slide, thumbs), title, mbProperties
.getProperty(title + ".sub"), mbProperties.getProperty(title + ".desc"), order));
}
}
Collections.sort(res, new ImageObjectOrderComparator());
return res;
}
private String findThumb(String slide, List<String> thumbs) {
String name = slide.substring(slide.lastIndexOf('/'));
if (name.endsWith(".flv")) {
name = name.replace(".flv", ".png");
}
for (String thumb : thumbs) {
if (thumb.endsWith(name)) {
return thumb;
}
}
throw new UncheckedRuntimeException("Multibox config is invalid.");
}
public int getCounter() {
return counter++;
}
public void setCounter(int counter) {
this.counter = counter;
}
}